package com.ayoka.parenting.ui.grown.detail;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.ayoka.parenting.R;
import com.ayoka.parenting.adapter.CoachGridAdapter;
import com.ayoka.parenting.bean.ClassroomBean;
import com.ayoka.parenting.bean.ClassroomDetailBean;
import com.ayoka.parenting.bean.GrownClassroomDetailBean;
import com.ayoka.parenting.databinding.ActivityGrownDetailBinding;
import com.ayoka.parenting.mvp.MVPBaseActivity;
import com.ayoka.parenting.ui.grown.share.GrownCourseShareActivity;
import com.ayoka.parenting.ui.live.liveclassroom.LiveClassroomActivity;
import com.ayoka.parenting.widget.MyCountDownTextView;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.DateUtil;
import com.jincp.common.utils.LiveDataBus;
import com.jincp.common.utils.MyLogger;
import com.jincp.common.utils.SharedPreferenceUtil;
import com.jincp.common.utils.StringUtil;
import com.jincp.image.load.ImageLoader;
import com.jincp.image.load.ImageLoaderOptions;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: GrownDetailActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class GrownDetailActivity extends MVPBaseActivity<ActivityGrownDetailBinding, GrownDetailContract.View, GrownDetailPresenter> implements GrownDetailContract.View {

    public static void start(Activity activity, String classroomId) {
        Intent intent = new Intent(activity, GrownDetailActivity.class);
        intent.putExtra("ClassroomId", classroomId);
        activity.startActivity(intent);
    }

    private String classroomId;
    private ClassroomDetailBean classroomDetailBean;

    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            classroomId = intent.getStringExtra("ClassroomId");
        }
    }


    @Override
    protected int getContentLayout() {
        return R.layout.activity_grown_detail;
    }

    @Override
    protected void initViews() {
        mDataBinding.tvCourseShare.setOnClickListener(this);
        mDataBinding.btnImmediatelyJoin.setOnClickListener(this);
        mDataBinding.tvCountDown.setOnClickListener(this);
        mDataBinding.tvCountDown.setEnabled(false);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(R.string.grown_course_detail);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        if (mPresenter != null) {
            mPresenter.getClassroomDetailData(classroomId);
        }
    }

    @Override
    protected GrownDetailPresenter getPresenter() {
        return new GrownDetailPresenter();
    }


    @Override
    public void onClick(View view) {
        if (view == mDataBinding.tvCourseShare) {
            GrownCourseShareActivity.start(this, classroomId);
        } else if (view == mDataBinding.tvCountDown) {
            if (classroomDetailBean != null && classroomDetailBean.nearestSchedule != null ) {
                mPresenter.createLiveRoom(classroomDetailBean.nearestSchedule.RoomID, classroomDetailBean.nearestSchedule.ID);
                mDataBinding.tvCountDown.setEnabled(false);
            }
        } else if (view == mDataBinding.btnImmediatelyJoin) {
            if (classroomDetailBean != null && classroomDetailBean.classRoom != null && classroomDetailBean.classRoom.LecturerInfo != null) {
                mPresenter.showConfirmJoinDialog(getSupportFragmentManager(),
                        classroomDetailBean.classRoom.ID,
                        classroomDetailBean.classRoom.LecturerInfo.CoachName,
                        classroomDetailBean.classRoom.IsOnTrial);
                mDataBinding.btnImmediatelyJoin.setEnabled(false);
            }
        }
    }


    @Override
    public void onDialogDismiss(DialogFragment dialog) {
        mDataBinding.btnImmediatelyJoin.setEnabled(true);
    }

    @Override
    public void onJoinClassroomCallback(boolean isSuccess) {
        if (isSuccess) {
            LiveDataBus.getInstance().with("GrownFragment", String.class).postValue("Notify");
            finishDelay();
        }
    }

    @Override
    public void configClassroomData(ClassroomDetailBean data) {
        if (data == null) return;
        this.classroomDetailBean = data;
        mDataBinding.btnImmediatelyJoin.setVisibility(data.isJoined > 0 ? View.GONE : View.VISIBLE);
        mDataBinding.tvCountDown.setVisibility(data.isJoined > 0 ? View.VISIBLE : View.GONE);
        if (classroomDetailBean.classRoom != null) {
            ClassroomBean classroomBean = classroomDetailBean.classRoom;
            mDataBinding.tvCourseName.setText(classroomBean.Title);
            mDataBinding.tvCourseStartDate.setText(StringUtil.formatString(R.string.format_start_date, DateUtil.get10Date(classroomBean.StartDate)));
            mDataBinding.tvCourseStartTime.setText(StringUtil.formatString(R.string.format_start_time, classroomBean.getStartNode() + " " + StringUtil.formatString("%02d:%02d", classroomBean.Hour, classroomBean.Min)));
            mDataBinding.tvCourseDescription.setText(classroomBean.Descp);
            if (classroomBean.LecturerInfo != null) {
                MyLogger.d(classroomBean.LecturerInfo.CoachAvatar);
                mDataBinding.tvCoachLabel.setText(classroomBean.LecturerInfo.CoachLabel);
                mDataBinding.tvCoachName.setText(classroomBean.LecturerInfo.CoachName);
                mDataBinding.tvCoachDescription.setText(classroomBean.LecturerInfo.CoachDescp);
                ImageLoader.getInstance().displayImage(classroomBean.LecturerInfo.CoachAvatar, mDataBinding.ivCoachHeader, ImageLoaderOptions.getRoundOptions());
                mDataBinding.tvCoachSuitable.setText(classroomBean.LecturerInfo.SuitableObject);
            }
            mDataBinding.recyclerViewCoach.setLayoutManager(new GridLayoutManager(this, 3));
            CoachGridAdapter coachGridAdapter = new CoachGridAdapter();
            coachGridAdapter.setNewData(mPresenter.getCoachListData());
            mDataBinding.recyclerViewCoach.setAdapter(coachGridAdapter);
        }
        if (classroomDetailBean.nearestSchedule != null) {
            GrownClassroomDetailBean nearestSchedule = classroomDetailBean.nearestSchedule;
            long endMillSecond = DateUtil.parseDate(nearestSchedule.StartTime).getTime();
            mDataBinding.tvCountDown.setEndMilliSeconds("倒计时：%s", endMillSecond, new MyCountDownTextView.CountDownListener() {
                @Override
                public void onTick(long endMillSecond) {
                    if (endMillSecond > 1800 * 1000) {
                        mDataBinding.tvCountDown.setEnabled(false);
                    } else {
                        mDataBinding.tvCountDown.setText(R.string.immediately_enter);
                        mDataBinding.tvCountDown.setEnabled(true);
                    }
                }
            });
        }
    }

    @Override
    public void onCreateLiveRoomCallback(boolean isSuccess, int roomId, String scheduleId) {
        if (isSuccess) {
            SharedPreferenceUtil.putInt("RoomId", roomId);
            SharedPreferenceUtil.putString("ScheduleId", scheduleId);
            LiveClassroomActivity.start(this);
        }
        mDataBinding.tvCountDown.setEnabled(true);
    }
}
